package com.ez.supervise.action.biz;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.core.Result;
import com.ez.supervise.model.biz.DemocracyGoal;
import com.ez.supervise.service.DemocracyGoalService;
import com.ez.supervise.util.JsonUtils;

public class DemocracyGoalAction extends BaseJsonAction {

    private static final long serialVersionUID = 6639654058416236294L;
    private Long id;
    private DemocracyGoalService democracyGoalService;
    private DemocracyGoal democracyGoal;
    private Page page;

    public String saveDemocracyGoal() {
	try {
	    if (democracyGoal.isNew()) {
		democracyGoalService.insert(democracyGoal);
	    } else {
		democracyGoalService.update(democracyGoal);
	    }
	    Result result = new Result(democracyGoal.getId());
	    result.setEntity(democracyGoal);
	    JsonConfig config = new JsonConfig();
	    config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new",
		"content" });
	    outString(JSONObject.fromObject(result, config).toString());
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String findAllDemocracyGoal() {
	try {
	    page = new Page();
	    List result = democracyGoalService.findAll();
	    page.setRoot(result);
	    page.setTotalProperty(result.size());
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));
	    
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }
    
    public String findDemocracyGoalByPage(){
	try {
	    page = new Page();
	    HttpServletRequest request = getRequest();
	    page.putCondition("year", NumberUtils.isNumber(request.getParameter("year")) ? Long.valueOf(request.getParameter("year")) : null);
	    page.putCondition("content", request.getParameter("content"));
	    int start = NumberUtils.toInt(request.getParameter("start"), 0);
	    int limit = NumberUtils.toInt(request.getParameter("limit"), 20);
	    page.setStart(start++);
	    page.setLimit(limit);
	    page.addPageInfo();
	    democracyGoalService.findDemocracyGoalByPage(page);
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));
	    
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }
    public String findAll() {
	page = new Page();
	page.setTotalProperty(0);
	page.setRoot(democracyGoalService.findAll());

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(page, config);
	// System.out.println(jsonObj.toString());
	outJsonString(jsonObj.toString());
	return SUCCESS;
    }

    /**
     * @return
     */
    public String deleteDemocracyGoal() {
	try {
	    if (id != null && id > 0) {
		democracyGoalService.delete(id);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * @return
     * @throws Exception
     */
    public String updateDemocracyGoal() throws Exception {
	String fieldName = getRequest().getParameter("fieldName");
	String fieldValue = getRequest().getParameter("fieldValue");
	String strDemocracyGoalId = getRequest().getParameter("id");
	try {
	    if (id != null && id > 0) {
		DemocracyGoal saveDemocracyGoal = new DemocracyGoal();
		saveDemocracyGoal.setId(id);
		BeanUtils.setProperty(saveDemocracyGoal, fieldName, fieldValue);
		democracyGoalService.update(saveDemocracyGoal);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}

	return SUCCESS;
    }

    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public static void main(String[] args) {
	DemocracyGoal democracyGoal = new DemocracyGoal();
	democracyGoal.setId(1L);
	// democracyGoal.setName("linlinyu");
	JSONObject json = JSONObject.fromObject(democracyGoal);

	System.out.println(json);

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(democracyGoal, config);
	System.out.println(jsonObj.toString());
    }

    public void setDemocracyGoalService(DemocracyGoalService democracyGoalService) {
	this.democracyGoalService = democracyGoalService;
    }

    public DemocracyGoal getDemocracyGoal() {
	return democracyGoal;
    }

    public void setDemocracyGoal(DemocracyGoal democracyGoal) {
	this.democracyGoal = democracyGoal;
    }

    public Page getPage() {
	return page;
    }

    public void setPage(Page page) {
	this.page = page;
    }

}
